package com.spic.business.plan.supplies.service.impl;

import com.spic.business.common.core.utils.poi.BusinessExcelUtil;
import com.spic.business.common.core.utils.poi.ExcelMsg;
import com.spic.business.common.core.utils.poi.ImportExcelParam;
import com.spic.business.common.datascope.anotation.BusinessDataScope;
import com.spic.business.plan.emergencycode.service.impl.EmergencyCodeServiceImpl;
import com.spic.business.plan.supplies.domain.PlanExcelDto;
import com.spic.business.plan.supplies.domain.PlanSupplies;
import com.spic.business.plan.supplies.mapper.PlanSuppliesMapper;
import com.spic.business.plan.supplies.service.IPlanSuppliesService;
import com.spic.common.core.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 应急物资台账Service业务层处理
 *
 * @author spic
 * @date 2021-10-27
 */
@Service
@Slf4j
public class PlanSuppliesServiceImpl implements IPlanSuppliesService {
    @Autowired
    private PlanSuppliesMapper planSuppliesMapper;
    @Autowired
    private EmergencyCodeServiceImpl emergencyCodeServiceImpl;

    /**
     * 查询应急物资台账
     *
     * @param id 应急物资台账主键
     * @return 应急物资台账
     */
    @Override
    public PlanSupplies selectPlanSuppliesById(Long id) {
        return planSuppliesMapper.selectPlanSuppliesById(id);
    }

    /**
     * 查询应急物资台账列表
     *
     * @param planSupplies 应急物资台账
     * @return 应急物资台账
     */
    @Override
    @BusinessDataScope(menuPath = "business/plan/supplies/index")
    public List<PlanSupplies> selectPlanSuppliesList(PlanSupplies planSupplies) {
        return planSuppliesMapper.selectPlanSuppliesList(planSupplies);
    }

    /**
     * 新增应急物资台账
     *
     * @param planSupplies 应急物资台账
     * @return 结果
     */
    @Override
    public int insertPlanSupplies(PlanSupplies planSupplies) {
        planSupplies.setCreateTime(DateUtils.getNowDate());
        String code = emergencyCodeServiceImpl.getCodeName();
        planSupplies.setMaterialCode(code);
        int rows = planSuppliesMapper.insertPlanSupplies(planSupplies);

        return rows;
    }

    @Override
    public int batchInsertSuplies(List<PlanSupplies> planSupplies) {
        Date now = DateUtils.getNowDate();
        for (PlanSupplies supplies : planSupplies) {
            supplies.setCreateTime(now);
            supplies.setMaterialCode(emergencyCodeServiceImpl.getCodeName());
        }
        return planSuppliesMapper.batchInsertSupplies(planSupplies);
    }

    /**
     * 修改应急物资台账
     *
     * @param planSupplies 应急物资台账
     * @return 结果
     */
    @Override
    public int updatePlanSupplies(PlanSupplies planSupplies) {
        planSupplies.setUpdateTime(DateUtils.getNowDate());
        return planSuppliesMapper.updatePlanSupplies(planSupplies);
    }

    /**
     * 批量删除应急物资台账
     *
     * @param ids 需要删除的应急物资台账主键
     * @return 结果
     */
    @Override
    public int deletePlanSuppliesByIds(Long[] ids) {
        return planSuppliesMapper.deletePlanSuppliesByIds(ids);
    }

    /**
     * 删除应急物资台账信息
     *
     * @param id 应急物资台账主键
     * @return 结果
     */
    @Override
    public int deletePlanSuppliesById(Long id) {
        return planSuppliesMapper.deletePlanSuppliesById(id);
    }


    private Set<String> getExistKeys() {
        List<String> planSupplies =  planSuppliesMapper.selectPlanSuppliesBusinessKey();
        return planSupplies.stream().collect(Collectors.toSet());
    }

    @Transactional
    public ExcelMsg importData(List<PlanExcelDto> planSuppliesList, boolean isUpdateSupport) throws InstantiationException, IllegalAccessException {
        ImportExcelParam<PlanSupplies, PlanExcelDto> param = new ImportExcelParam();
        param.setImportDataList(planSuppliesList)
                .setUpdateSupport(isUpdateSupport).setClaz(PlanSupplies.class)
                .setBatchInsertMethod(planSuppliesMapper::batchInsertSupplies)
                .setUpdateMethod(planSuppliesMapper::updatePlanSuppliesByBusinessKey)
                .setGetExistKeysMethod(this::getExistKeys);
        return BusinessExcelUtil.importData(param);
    }
}
